import utils from '@/mixins/utils.js';
export default {
	mixins: [utils],
	data() {
		return {
			parkBaseInfo: null,
			domain: null,
			curUploadImg: null,
			parkBaseInfoRules: {
				chargeRulePicStr: [{ required: true, message: '请上传收费规则图片', trigger: 'blur' }]
			}
		};
	},
	created() {
		this.getOperatorParkBasicCharge();
	},
	methods: {
		getOperatorParkBasicCharge() {
			const self = this;
			var params = {
				parkID: this.$route.query.parkId
			};
			API.fetchGet('/operatorPark/getOperatorParkBasicCharge', params).then(
				res => {
					if (res.data) {
						this.domain = res.data.domain;
						this.parkBaseInfo = JSON.parse(JSON.stringify(res.data.parkBaseInfo));
						this.$nextTick(() => {
							this.bindUploadEvent();
						});
					}
				},
				err => {}
			);
		},
		back() {
			this.$router.push({
				name: 'allParkinglotInfo',
				query: this.$route.query
			});
		},
		checkUnit(rule, value, callback) {
			const integer_reg = /^[1-9]\d*$/;
			if (value === '') {
				callback(new Error('时间不能为空'));
			} else if (!integer_reg.test(value)) {
				callback(new Error('必须是大于0的整数'));
			} else {
				callback();
			}
		},
		selectImgFile() {
			$('#upload-input').click();
		},
		bindUploadEvent() {
			$(document).on('change', '#upload-input', async e => {
				console.log(e.target.files[0]);
				const isJPG = e.target.files[0].type === 'image/jpeg' || e.target.files[0].type === 'image/png';
				const isLt900K = e.target.files[0].size / 1024 / 1024 < 0.87;
				if (!isJPG) {
					this.showToast('上传图片只能是 JPG 格式或者 PNG 格式!', 2, 1500);
					return;
				}
				if (!isLt900K) {
					this.showToast('上传图片大小不能超过 900K!', 2, 1500);
					return;
				}

				const imgData = await this.getUploadImgData(e.target.files[0]);
				this.curUploadImg = imgData.base64Str;
				this.parkBaseInfo.chargeRulePicStr = imgData.base64Str.split(',')[1];
				this.parkBaseInfo.chargeRulePicType = imgData.imgType;
				this.parkBaseInfo.chargePicChangeFlag = true;
			});
		},
		getUploadImgData(file) {
			return new Promise((resolve, reject) => {
				const reader = new FileReader();
				reader.readAsDataURL(file);
				reader.onload = e => {
					const result = {
						base64Str: e.target.result,
						imgType: file.type.split('/')[1]
					};
					resolve(result);
				};
			});
		},
		testParkChargeRulelImg() {
			return new Promise((resolve, reject) => {
				this.$refs['parkBaseInfo'].validate(valid => {
					if (valid) {
						resolve(true);
					} else {
						resolve(false);
					}
				});
			});
		},
		async saveEdit() {
			const testRuleImg = await this.testParkChargeRulelImg();
			if (testRuleImg) {
				let saveData = {
					parkBaseInfo: this.parkBaseInfo,
					loginUserId: window.sessionStorage.getItem('loginid')
				};
				API.fetchPost('/operatorPark/saveOperatorParkBasicChargePicInfo', saveData).then(
					res => {
						if (res.status === 1) {
							this.showToast('操作成功', 1, 1500);
							this.back();
						} else {
							this.showToast('操作失败!', 2, 1500);
						}
					},
					err => {
						console.log(err);
					}
				);
			}
		}
	}
};
